$(document).ready(
	function()
	{	$(".datePicker").datepicker();
		$(".ajaxGrid").load("index.php", {"__ajax": $(".ajaxGrid").attr("id"), "sys_action": "loadGrid", "gridID": $(".ajaxGrid").attr("gridID")});
		$(".loadAjax").load("index.php", {"__ajax": $(".loadAjax").attr("id"), "sys_action": $(".loadAjax").attr("id")});
		$(".loadAjax2").load("index.php", {"__ajax": "", "sys_action": $(".loadAjax2").attr("id"), "prodavac": $("#prodavac").val()});
		$("#empImg").click(
		 function(){
		  $("#upload").fadeIn();
		 }
		);
	
		$("#__ajax_slika").click(
		  function()
		  {	$("#stSliku").submit();
			  $("#upload").fadeOut();
		  }
		);
	
		$("#kliza").slider();
		//$("#pkliza").slider();//
	}
);

function handleWidgetEvent(sender, event, place, page)
{
	$.get("index.php", { "__ajax": sender.value, "sys_action": event},
	function(data)
	{	//alert(data);
		$(".ajaxGrid").html(data);
	});
}

//---------------->
function getPage(obj, page)
{ if(!page)
	{	page = "index.php";
	}
	if(page.indexOf("?") > 0)
	{	document.location.href = page + "&" + obj.name + "=" + obj.value;
	}	else
	{	document.location.href = page + "?" + obj.name + "=" + obj.value;
	}
}

function submitForm_old(obj, form)
{ if(!form)
	{	$("form").submit();
	}
	else
	{	document.getElementById(form).submit();
	}

}
function submitForm(obj, form)
{ var str=$('input, textarea, select').serialize();

  if(!form)
  {	$("form").submit();
	}
	else
	{	document.getElementById(form).submit();
	}
}




function submitForm2Ajax(obj, page, idToFill)
{ var str=$('input, textarea, select').serialize();

	$("#bee_progress_bar").html("...");
	$.get(obj.form.action +"?"+ page + "&"+ str,
	function(data)
	{			$(idToFill).html(data);
				$("#status").html($("#error").val());
				$("#status").attr("class", $("#error_style").val());
				//	alert($("#zerrort"))
				$("#bee_progress_bar").html(".");;
				return false;
			});
}

function submitFormEvt2Ajax(obj, funct, idToFill)
{ 	return submitForm2Ajax(obj, "__ajax=&sys_action=" + funct, idToFill);
}

function loadAjax(obj, page, idToFill)
{	var url;
	jQuery.ajaxSetup({ contentType: "text/html;;charset=utf-8" });
	if(page.indexOf("?") > 0)
	{	url = page + "&" + obj.name + "=" + obj.value;
	}	else
	{	url = page + "?" + obj.name + "=" + obj.value;
	}
	//alert(idToFill + " :::: " + url);

	$.ajax(	{url: 		url,
		type: "POST",
		contentType: "application/x-www-form-urlencoded;charset=UTF-8",
		success:	function(data)
			{	//alert(data);
		$(idToFill).html(data);
	}
	});
	/*
	$.get(url, function(data)
			{	//alert(data);
				$(idToFill).html(data);
			}
		);*/
	//$(idToFill).html("<tr><td>***</td></tr>");

}

function loadEvt2Ajax(obj, page, params, idToFill)
{	var url, widgetValue;
	if(obj.value)
	{	widgetValue = escape(obj.value);
	}
	if(obj.text)
	{	widgetValue = escape(obj.text);
	}
	//alert(idToFill);
	var url = "index.php?__ajax=&sys_action=" + page + "&" + params;
    loadAjax(obj, url, idToFill);
}

var selected=0;
function hideHelper(idH)
{	$(idH).css("visibility: hidden");
}

function helpMe(event, obj, helperID)
{ if($("#"+ helperID).length == 0)
  {	var toop = $("#" + obj.id).offset().top + $("#" + obj.id).outerHeight()+2;
	var leeft = $("#" + obj.id).offset().left;
	$('<div id="' + helperID + '" style="position:absolute; z-index:99; border:dotted thin #777; background-color:#fff">...</div>').appendTo("body").css("top",toop);
	$("#" + helperID).css("left", leeft);
	TC = function(objid)
	{	$("#"+ helperID + " td").click(
			function()
			{	$("#" + helperID).css("visibility", "hidden");
				$("#" + obj.id).val($(".helperTable td:eq(" + selected + ")").html());
				alert("click");
				$("#" + objid).val($(this).text());
				selected = 0;
				//alert($(this).parent().children().index(this));
			}
		);
	};
	//alert(obj.id);
	TC(obj.id);
  }
  var pKey= new Number;

  if (window.event)
  {	pKey = window.event.keyCode;
  }	else if(event)
  {	pKey = event.which;
  }	else
  {	return true;
  }
  if(event.keyCode == 38)//window.status = "-.-";
  {	if(selected > 0)
  	{	$(".helperTable td:eq(" + selected + ")").css("background-color", "#fff");
		selected--;
		$(".helperTable td:eq(" + selected + ")").css("background-color", "#ccc");
	}
  } else if(event.keyCode == 40)
  { if(selected < $(".helperTable td").length - 1)
	{	$(".helperTable td:eq(" + selected + ")").css("background-color", "#fff");
		selected++;
		$(".helperTable td:eq(" + selected + ")").css("background-color", "#ccc");
	}
} else if(event.keyCode == 27)
{ $("#" + helperID).css("visibility", "hidden");
} else if(event.keyCode == 13)
{	if($("#" + helperID).css("visibility") == "visible")
	{	$("#" + helperID).css("visibility", "hidden");
		//alert(selected + " / " + obj.id + " / " + $(".helperTable td:eq(" + selected + ")").html());
		$("#" + obj.id).val($(".helperTable td:eq(" + selected + ")").text());
		selected = 0;
	}
	return true;
} else
{  $.get("index.php?__ajax=helper&sys_action=drawMe&__filter=" + obj.value,
				function(data)
				{	//alert(data);
					$("#" + helperID).html(data);
					$(".helperTable td:eq(" + selected + ")").css("background-color", "#ccc");
					if($("#" + helperID + " td").length == 0)
					{ $("#" + helperID).css("visibility", "hidden");
					} else
					{ $("#" + helperID).css("visibility", "visible");
					}
					TC(obj.id);
				}
			);

	//$("#" + helperID).css("visibility", "visible");
	selected = 0;
}
}

function getHelp(event, obj, page, idToFill)
{	//if($(""))
	var pKey= new Number;
	if (window.event)
	{	pKey = window.event.keyCode;
	}	else if(event)
	{	pKey = event.which;
	}	else
	{	return true;
	}
	$("#textfield").val(pKey);////////////IZBRISATI-DEBUG INFO
	if(event.keyCode == 38)//window.status = "-.-";
	{	if(selected > 0)
		{	$(".helperTable td:eq(" + selected + ")").css("background-color", "#fff");
			selected--;
			$(".helperTable td:eq(" + selected + ")").css("background-color", "#ccc");
		}
	}
	else if(event.keyCode == 40)
	{ if(selected < $(".helperTable td").length - 1)
		{	$(".helperTable td:eq(" + selected + ")").css("background-color", "#fff");
			selected++;
			$(".helperTable td:eq(" + selected + ")").css("background-color", "#ccc");
		}
	}
	else if(event.keyCode == 27)
	{ $(idToFill).css("visibility", "hidden");

	}
	else if(event.keyCode == 13)
	{	if($(idToFill).css("visibility") == "visible")
		{	$(idToFill).css("visibility", "hidden");
			//alert(obj.id + " / " + $("#helperTable td:eq(" + selected + ")").html());
			$("#" + obj.id).val($("#helperTable td:eq(" + selected + ")").html());
			selected = 0;

		}
		$(this).log("aa");
		return true;
	}
	else
	{	if(page.indexOf("?") > 0)
		{	//$(idToFill).load(page + "&__filter=" + obj.value);
			$.get(page + "&__filter=" + obj.value,
					function(data)
					{	$(idToFill).html(data);
						$(".helperTable td:eq(" + selected + ")").css("background-color", "#ccc");
					}
				);
		}	else
		{	$(idToFill).load(page + "?__filter=" + obj.value);
		}
		$(idToFill).css("visibility", "visible");
		selected = 0;
	}


}
//---------------->


function handleGridEvent(evt)
{ $.get("index.php", evt,
	function(data)
	{	//alert(data);
		$(".ajaxGrid").html(data);
	});

}

function handleGridEvent2(evt)
{ for(var key in evt)
	{ alert(key + "/" + evt[key]);
	}
}

function AIMstartCallback() {
	// make something useful before submit (onStart)
	//alert("prije");
	return true;
}

function AIMcompleteCallback(response) {
	//$("#empImg").attr("src", "");
	$("#empImg").attr("src", "images/img_" + $("#id").attr("value") + ".jpg?" + Math.random());
	$("#upload").fadeOut("slow");


	// make something useful after (onComplete)
	//document.getElementById('nr').innerHTML = parseInt(document.getElementById('nr').innerHTML) + 1;
	//document.getElementById('r').innerHTML = response;
}
